package com.itheima.web.controller;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.util.JwtUtil;
import com.itheima.vo.ErrorResult;
import io.jsonwebtoken.Jwt;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.Map;
import java.util.PrimitiveIterator;

@RestController
public class LoginTest {

    private UserService userService;

    @RequestMapping(value = "/weixinlogin")
    public ResponseEntity Login(String code){
        User user = userService.wxLogin(code) ;
        if(user==null){
            //return new Result(false,StatusCode.LOGINERROR,"微信登录失败");
            return ResponseEntity.status(500).body(ErrorResult.error());
        }
        Map<String,String> map=new HashMap<>();
        //String token = jwtUtil.createJWT(user.getId(), user.getNickname(), "user");
        map.put("name", user.getNickname());
        map.put("avatar",user.getAvatar());//头像
        String token = JwtUtil.createToken(map);
        map.put("token",token);
        //return new Result(true,StatusCode.OK,"登陆成功",map);
        return ResponseEntity.ok(map);
    }
}
